package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.bean.MyImgBean;
import flc.ast.databinding.ActivityLocalPicBinding;
import flc.ast.utils.MhzDateUtils;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class LocalPicActivity extends BaseAc<ActivityLocalPicBinding> {

    private SelPictureAdapter selPictureAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_local_pic;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivLocalPicBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvPictureList.setLayoutManager(new LinearLayoutManager(mContext));
        selPictureAdapter = new SelPictureAdapter();
        mDataBinding.rvPictureList.setAdapter(selPictureAdapter);
        selPictureAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        getPermission();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        if (StkPermissionHelper.hasGrantManageExternalStoragePermission(mContext)) {
//            getData();
//        } else {
//            getPermission();
//        }
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.reqManageExternalStoragePermission()
                .reqPermissionDesc(getString(R.string.get_pic_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvPictureList.setVisibility(View.GONE);
                        mDataBinding.tvPictureNoData.setVisibility(View.GONE);
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        showDialog(getString(R.string.loading));
        RxUtil.create(new RxUtil.Callback<List<MyImgBean>>() {
            @Override
            public void doBackground(ObservableEmitter<List<MyImgBean>> emitter) {
                List<String> list = new ArrayList<>();
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                for (SelectMediaEntity bean : listSys) {
                    list.add(bean.getPath());
                }
                List<MyImgBean> dayData = MhzDateUtils.getDayData(list);
                emitter.onNext(dayData);
            }

            @Override
            public void accept(List<MyImgBean> list) {
                dismissDialog();
                if (list != null && list.size() > 0) {
                    selPictureAdapter.setList(list);
                    mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
                    mDataBinding.tvPictureNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPictureList.setVisibility(View.GONE);
                    mDataBinding.tvPictureNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}